// src/router.js
import loginVue from "./components/login.vue"
import {createRouter,createWebHashHistory} from "vue-router"
import homeVue from "./components/home.vue"
import MyUsersVue from "./components/subcomponents/MyUsers.vue"
import MyUserDetailVue from "./components/user/MyUserDetail.vue"
import MyRightsVue from "./components/subcomponents/MyRights.vue"
import MyGoodsVue from "./components/subcomponents/MyGoods.vue"
import MyOrdersVue from "./components/subcomponents/MyOrders.vue"
import MySettingsVue from "./components/subcomponents/MySettings.vue"
import MyMenuVue from "./components/subcomponents/MyMenu.vue"
const router = createRouter({
    history: createWebHashHistory(),
    routes: [
        {
            path: "/login",
            component: loginVue
        },
        {
            path:"/",
            redirect:"/login"
        },
        {
            path:"/home",
            component:homeVue,
            redirect:"/home/users",
            children: [
                {
                    path:"users",
                    component:MyUsersVue
                },
                {
                    path:'users/:id/:name/:level',// 动态参数部分
                    component:MyUserDetailVue
                },
                { 
                    path: 'rights', 
                    component:MyRightsVue
                },
                { 
                    path: 'goods', 
                    component:MyGoodsVue
                },
                { 
                    path: 'orders', 
                    component: MyOrdersVue
                },
                { 
                    path: 'settings', 
                    component: MySettingsVue
                },
                { 
                    path: 'menu', 
                    component:MyMenuVue
                },
            ]
        }
    ]
})
export default router

router.beforeEach((to, from, next) => {
  // 如果访问的是登录页面，直接执行下一个钩子函数
  if(to.path === '/login')
  return next()
  // 获取Token值
  const token = localStorage.getItem('token')
  if (!token) {
    // Token值不存在，强制跳转到登录页面
    return next('/login')
  }
  // 存在Token值，直接执行下一个钩子函数
  next()
})